import java.io.*;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


@SuppressWarnings("serial")
public class ItemServlet extends HttpServlet{

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		Document itemXmlFromCloud;
		PrintWriter out = response.getWriter();
		Namespace nameSpace = Namespace.getNamespace("http://www.cs.au.dk/dWebTek/2011");

        try {
                itemXmlFromCloud = new SAXBuilder().build("http://services.brics.dk/java/cloud/listItems?shopID=16");
                List<Element> items = itemXmlFromCloud.getRootElement().getChildren("item", nameSpace);
                
                //experiment with different approach to constructing html
                alternativeBuildMethod(out, nameSpace, items);

        } catch (JDOMException e) {
                e.printStackTrace();
        }
}

	private void alternativeBuildMethod(PrintWriter out, Namespace nameSpace, List<Element> items) throws IOException {
		Element table = new Element("table");
		Document itemsXml = new Document(table);

		for(Element item : items){
			//One row for each item listed
			Element tr = new Element("tr");
			table.addContent(tr);

			{
				// A cell for the picture
				Element td = new Element("td");
				tr.addContent(td);

				{
					//image
					Element img = new Element("img");
					img.setAttribute("id", "itemPicture");
					img.setAttribute("class","itemPicture");
					img.setAttribute("src", item.getChildText("itemURL", nameSpace));
					td.addContent(img);
				}

				// A cell for item information
				td = new Element("td");
				tr.addContent(td);
				{
					//item name
					Element div = new Element("div");
					div.setAttribute("class", "itemName");
					div.setText(item.getChildText("itemName", nameSpace));
					td.addContent(div);

					//description
					td.addContent(new Text(item.getChildText("itemDescription",nameSpace)));

					//table to hold price, stock and price
					Element table2 = new Element("table");
					td.addContent(table2);
					{
						tr = new Element("tr");
						table2.addContent(tr);
						{
							td = new Element("td");
							tr.addContent(td);
							{
								Element i = new Element("i");
								i.setText("Price:" + item.getChildText("itemPrice", nameSpace));
								td.addContent(i);
							}
							td = new Element("td");
							tr.addContent(td);
							{
								Element i = new Element("i");
								i.setText("Stock:" + item.getChildText("itemStock", nameSpace));
								td.addContent(i);
							}

							td = new Element("td");
							tr.addContent(td);
							{
								//Buy button
								Element form = new Element("form");
								form.setAttribute("action" , "addToCart");
								form.setAttribute("method","post");
								td.addContent(form);

								{
									//hidden name
									Element input = new Element("input");
									input.setAttribute("type", "hidden");
									input.setAttribute("name", "itemName");
									input.setAttribute("value",item.getChildText("itemName",nameSpace));
									form.addContent(input);
									
									//hidden itemID
									input = new Element("input");
									input.setAttribute("type", "hidden");
									input.setAttribute("name", "itemID");
									input.setAttribute("value",item.getChildText("itemID",nameSpace));
									form.addContent(input);

									//amount text
									input.addContent(new Text("Amount"));

									//amount input
									input = new Element("input");
									input.setAttribute("type", "text");
									input.setAttribute("name", "amount");
									input.setAttribute("size","2px");
									form.addContent(input);
									
									//submit button
									input = new Element("input");
									input.setAttribute("type", "submit");
									input.setAttribute("value", "Add to cart");
									input.setAttribute("size", "30px");
									form.addContent(input);
								}
							}
						}
					}
				}
			}
		}

		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
		outputter.output(itemsXml, out);
	}

}